package it.eng.area118.mdogpscollector.activity;

import it.eng.area118.mdocommon.Callback;
import it.eng.area118.mdocommon.android.util.AndroidApplicationContext;
import it.eng.area118.mdocommon.geolocation.Geolocation;
import it.eng.area118.mdocommon.geolocation.GeolocationCallback;
import it.eng.area118.mdocommon.geolocation.GpsLocation;
import it.eng.area118.mdogpscollector.ItemListActivity;
import it.eng.area118.mdogpscollector.R;
import it.eng.area118.mdogpscollector.command.UpdateCheckPointCommand;
import it.eng.area118.mdogpscollector.fragment.CheckpointDetailFragment;
import it.eng.area118.mdogpscollector.listener.FragmentActionReceiver;
import it.eng.area118.mdogpscollector.listener.ManageCheckPointListener;
import it.eng.area118.mdogpscollector.model.CheckPoint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.MenuItem;

public class CheckPointDetailActivity extends FragmentActivity implements FragmentActionReceiver<ManageCheckPointListener> {

	private CheckpointDetailFragment detailFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_item_detail);

		getActionBar().setDisplayHomeAsUpEnabled(true);

		if (savedInstanceState == null) {
			Bundle arguments = new Bundle();
			arguments.putString(CheckpointDetailFragment.CHECKPOIN_ID, getIntent().getStringExtra(CheckpointDetailFragment.CHECKPOIN_ID));
			detailFragment = new CheckpointDetailFragment();
			detailFragment.setArguments(arguments);
			getSupportFragmentManager().beginTransaction().add(R.id.item_detail_container, detailFragment).commit();
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			NavUtils.navigateUpTo(this, new Intent(this, ItemListActivity.class));
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public ManageCheckPointListener getActionListener() {
		return new ManageCheckPointListener() {

			private GeolocationCallback currentCallback;

			@Override
			public void onCheckPointSelected(String id) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onStartCoordCapture() {
				Log.i("cHECKpOINTdETAILacTIVITY", ">>>>onStartCoordCapture2");
				Geolocation geolocation = (Geolocation) AndroidApplicationContext.getInstance().getApplicationContextValue(Geolocation.GEOLOCATION_SERVICE);
				if (!geolocation.isActive()) {
					geolocation.start();

				}
				currentCallback = new GeolocationCallback() {

					@Override
					public void onLocationReceived(GpsLocation location) {
						Log.i("", "onLocationReceived: " + location);
						if (detailFragment != null) {
							detailFragment.updateCurrentLocation(location);
						}
					}
				};
				geolocation.watchForLocation(currentCallback);

			}

			@Override
			public void onSaveCheckPointCoord(CheckPoint cp) {
				UpdateCheckPointCommand cmd = new UpdateCheckPointCommand();
				cmd.execute(new Callback<CheckPoint>() {

					ProgressDialog dialog;

					@Override
					public void onBeforeExecute() {
						Log.d("UpdateCheckPointCommand", "onBeforeExecute");
						dialog = ProgressDialog.show(AndroidApplicationContext.getInstance().getApplicationContext(), "", "Salvataggio in corso...", true, false);
					}

					@Override
					public void onSuccess(CheckPoint result) {
						Log.d("UpdateCheckPointCommand", "onSuccess " + result.toString());
						detailFragment.setCheckPoint(result);
						dialog.dismiss();

					}

					@Override
					public void onFailure(Throwable e) {
						Log.d("UpdateCheckPointCommand", "onFailure", e);
						dialog.dismiss();
					}

					@Override
					public void onCancelled() {
						Log.d("UpdateCheckPointCommand", "onCancelled");
						dialog.dismiss();
					}

				}, cp);

			}

			@Override
			public void onStopCoordCapture() {
				// TODO Auto-generated method stub

			}

		};
	}
}
